package com.trs.publish.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trs.publish.domain.PowerCatalog;
import com.trs.publish.domain.SearchParam;
import com.trs.publish.service.PowerCatalogService;


/**
 * 权限分类管理
 * @author jia.haihong
 *
 */

@Controller
public class PowerCatalogController{ 
	
	private static final Logger logger = LoggerFactory.getLogger(PowerCatalogController.class);
	
	@Autowired 
	private PowerCatalogService powerCatalogService;

	@RequestMapping(value = "/toPowerCatalog")
    public ModelAndView toPowerCatalog(ModelAndView mav) {
        mav.setViewName("powerCatalog/list");
        return mav;
    }  
	
	@RequestMapping(value = "/powerCatalog", method = RequestMethod.POST)
    @ResponseBody
    public Object search(@RequestBody SearchParam searchParam) {
		String catalogname = searchParam.getCatalogname();
		String startTime = searchParam.getStartTime();
		String endTime = searchParam.getEndTime();
		
		//根据查询条件获取用户列表
		PowerCatalog powerCatalog = new PowerCatalog();
		powerCatalog.setCatalogname(catalogname);
		powerCatalog.setStartTime(startTime);
		powerCatalog.setEndTime(endTime);
		
		//mybatis分页
		PageHelper.startPage(searchParam);
		List<PowerCatalog> list = powerCatalogService.getList(powerCatalog);
		PageInfo<PowerCatalog> pageInfo = new PageInfo<PowerCatalog>(list);
        return pageInfo;
    }
	
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/powerCatalog/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Object delete(@PathVariable("id") long id) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.debug("删除资源分类id=" + id);
		powerCatalogService.deletePowerCatalog(id);
		map.put("message", "删除资源分类成功！");
		return map;
	}
	
	/**
	 * 查询单个资源分类
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/powerCatalog/{id}", method = RequestMethod.GET)
	public ModelAndView get(ModelAndView mav,@PathVariable("id") long id) {
		logger.debug("查询单个资源分类id=" + id);
		PowerCatalog powerCatalog = new PowerCatalog();
		//获取用户信息
		if(id==0){
			powerCatalog.setId(id);
		}else{
			powerCatalog = powerCatalogService.findById(id);
		}
		//一级分类列表
		long pid = 0;
		List<PowerCatalog> topPowerCatalogs = powerCatalogService.queryByPid(pid);
		mav.addObject("topPowerCatalogs", topPowerCatalogs);
		mav.addObject("powerCatalog", powerCatalog);
		mav.setViewName("powerCatalog/detail");
		return mav;
	}
	
	/**
	 * 查询子节点资源分类
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/powerCatalog/children/{parentid}", method = RequestMethod.GET)
	@ResponseBody
	public Object getByPid(@PathVariable("parentid") long parentid) {
		logger.debug("查询子节点资源分类id=" + parentid);
		List<PowerCatalog> list = powerCatalogService.queryByPid(parentid);
		return list;
	}
	
	/**
	 * 更新/新增
	 * 
	 * @param powerCatalog
	 * @return
	 */
	@RequestMapping(value = "/powerCatalog", method = RequestMethod.PUT)
	@ResponseBody
	public Object addOrUpdate(@RequestBody PowerCatalog powerCatalog) {
		Map<String, Object> map = new HashMap<String, Object>();
		StringBuffer message = new StringBuffer();
		
		long id = powerCatalog.getId();
		//判断分类名称
		if(StringUtils.isEmpty(powerCatalog.getCatalogname())){
			message.append("分类名称不能为空。");
		}
		
		if(StringUtils.isEmpty(message)){
			if(id == 0){//新增方法
				//重置id
				powerCatalog.setId(null);
				powerCatalogService.save(powerCatalog);
			}else{//修改方法
				powerCatalogService.update(powerCatalog);
			}
		}
		map.put("message", message.toString());
		return map;
	}
}

